package com.example.wechat;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class ChatActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private EditText editMessage;
    private ImageButton btnSend;
    private MessageAdapter messageAdapter;
    private String currentUserId = "user1"; // 临时使用固定ID，实际应该从登录信息获取

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        initViews();
        initToolbar();
        setupRecyclerView();
        setupListeners();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recyclerView);
        editMessage = findViewById(R.id.editText);
        btnSend = findViewById(R.id.btnSend);
    }

    private void initToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        
        // 获取传递的联系人信息
        Contact contact = getIntent().getParcelableExtra("contact");
        String contactName = getIntent().getStringExtra("contact_name");
        
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(contactName != null ? contactName : "聊天");
        }
    }

    private void setupRecyclerView() {
        messageAdapter = new MessageAdapter(currentUserId);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(messageAdapter);
        
        // 添加一些测试消息
        List<Message> messages = new ArrayList<>();
        messages.add(new Message(UUID.randomUUID().toString(), "user2", currentUserId, "你好！", new Date(), true));
        messages.add(new Message(UUID.randomUUID().toString(), currentUserId, "user2", "你好！最近怎么样？", new Date(), true));
        messages.add(new Message(UUID.randomUUID().toString(), "user2", currentUserId, "我很好，谢谢关心！", new Date(), true));
        messageAdapter.submitList(messages);
    }

    private void setupListeners() {
        btnSend.setOnClickListener(v -> sendMessage());
    }

    private void sendMessage() {
        String content = editMessage.getText().toString().trim();
        if (!content.isEmpty()) {
            Message message = new Message(
                    UUID.randomUUID().toString(),
                    currentUserId,
                    "user2",
                    content,
                    new Date(),
                    false
            );
            List<Message> currentList = new ArrayList<>(messageAdapter.getCurrentList());
            currentList.add(message);
            messageAdapter.submitList(currentList);
            recyclerView.scrollToPosition(currentList.size() - 1);
            editMessage.setText("");
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 